package cn.daniel.video.service.impl;

import cn.daniel.commonutils.exceptionhandler.exception.MyException;
import cn.daniel.commonutils.result.ResultCode;
import cn.daniel.video.constants.InitAliyunClient;
import cn.daniel.video.constants.VideoConstants;
import cn.daniel.video.service.VodService;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {


    @Override
    public String uploadVideo(MultipartFile file) {
        try{
            String filename = file.getOriginalFilename();
            String title = filename.substring(0, filename.lastIndexOf("."));
            String videoId=null;
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest uploadStreamRequest = new UploadStreamRequest(VideoConstants.KEY_ID, VideoConstants.KEY_SECRET, title, filename, inputStream);
            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(uploadStreamRequest);
            videoId=response.getVideoId();

            return videoId;
        }catch (Exception e){
            e.printStackTrace();
            throw MyException.setException(ResultCode.UPLOAD_FILE_ERROR);
        }

    }

    @Override
    public void deleteBatchVideo(List<String> videoIdList) {
        try {
            DefaultAcsClient client = InitAliyunClient.initVodClient(VideoConstants.KEY_ID, VideoConstants.KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            //将videoIdList转化为1,2,3·····的字符串作为videoIds
            String videoIds= StringUtils.join(videoIdList.toArray(),",");
            //向request设置视频id
            request.setVideoIds(videoIds);
            //用client删除
            client.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
            throw MyException.setException(ResultCode.DELETE_FILE_ERROR);
        }
    }

    @Override
    public HashMap<String, Object> getVideoInfo(String videoId) {
        HashMap<String, Object> map = new HashMap<>();
        DefaultAcsClient client = InitAliyunClient.initVodClient(VideoConstants.KEY_ID, VideoConstants.KEY_SECRET);
        GetVideoInfoRequest request = new GetVideoInfoRequest();
        request.setVideoId(videoId);
        try {
            GetVideoInfoResponse response=client.getAcsResponse(request);
            map.put("title",response.getVideo().getTitle());
            map.put("duration",response.getVideo().getDuration());
            map.put("videoSize",response.getVideo().getSize());
            map.put("status",response.getVideo().getStatus());
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return map;
    }


}
